/**
 * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.z100h.supplychain.core.biz.service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeeplus.core.service.CrudService;
import com.jeeplus.modules.sys.entity.User;
import com.jeeplus.modules.sys.utils.UserUtils;
import com.z100h.supplychain.core.ResourceLockState;
import com.z100h.supplychain.core.biz.entity.ResourceLog;
import com.z100h.supplychain.core.biz.mapper.ResourceLogMapper;

/**
 * 产品资源生命周期Service
 *
 * @author madara
 * @version 2019-01-04
 */
@Service
@Transactional(readOnly = true)
public class ResourceLogService extends CrudService<ResourceLogMapper, ResourceLog> {

	@Autowired
	private ResourceLogMapper resourceLogMapper;

	/**
	 * 批量插入资源日志
	 *
	 * @param resourceLog
	 */
	@Transactional(readOnly = false)
	public void addBatchResourceLog(ResourceLog resourceLog) {
		resourceLog.preInsert();
		resourceLogMapper.addBatchResourceLog(resourceLog);
	}

	/**
	 * 根据凭据锁查询资源
	 *
	 * @param evidenceId
	 * @return
	 */
	@Transactional(readOnly = false)
	public List<ResourceLog> findResourceLogByEvidenceId(String evidenceId) {
		return resourceLogMapper.findResourceLogByEvidenceId(evidenceId);
	}

	/**
	 * 资源日志确认（入库用）
	 * 
	 * @param resourceIds
	 * @author: Elvin Zhao - 279152260@qq.com 
	 * @date: Created on 2019年1月5日 下午2:23:00
	 */
	@Transactional(readOnly = false)
	public void confirmResourceLogging(List<String> resourceIds, String evidenceId) {
		Map<String, Object> param = new HashMap<String, Object>();
		User user = UserUtils.getUser();
		for (String resourceId : resourceIds) {
			param.put("resourceId", resourceId);
			param.put("evidenceId", evidenceId);
			param.put("resourceLockState", ResourceLockState.UNLOCK.getValue());
			param.put("updateBy", user.getId());
			param.put("updateDate", new Date());
			// TODO
			resourceLogMapper.confirmResourceLogging(param);
		}
	}

	/**
	 * 资源日志确认（入库用）
	 * 
	 * @param resourceIds
	 * @author: Elvin Zhao - 279152260@qq.com 
	 * @date: Created on 2019年1月5日 下午2:23:00
	 */
	@Transactional(readOnly = false)
	public void confirmResourceLoggingByEid(String evidenceId) {
		Map<String, Object> param = new HashMap<String, Object>();
		User user = UserUtils.getUser();
		param.put("evidenceId", evidenceId);
		param.put("resourceLockState", ResourceLockState.UNLOCK.getValue());
		param.put("updateBy", user.getId());
		param.put("updateDate", new Date());
		resourceLogMapper.confirmResourceLoggingByEid(param);
	}

	/**
	 * 检查单子对应的确认状态 
	 * 
	 * @param evidenceId
	 * @author: Elvin Zhao - 279152260@qq.com 
	 * @date: Created on 2019年1月5日 下午2:26:48
	 */
	@Transactional(readOnly = false)
	public Integer checkConfirmResourceLogging(String evidenceId) {
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("evidenceId", evidenceId);
		param.put("resourceLockState", ResourceLockState.LOCK.getValue());
		return resourceLogMapper.checkConfirmResourceLogging(param);
	}

	/**
	 * 添加资源日志表
	 * @param evidenceId
	 * @param resourceLockState
	 */
	@Transactional(readOnly = false)
	public void resourceLogging(final String evidenceId, final ResourceLockState resourceLockState) {
		User user = UserUtils.getUser();
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("evidenceId", evidenceId);
		param.put("resourceLockState", resourceLockState.getValue());
		param.put("createBy", user.getId());
		param.put("updateDate", new Date());
		resourceLogMapper.resourceLogging(param);
	}

	/**
	 * 
	 * 出库日志记录
	 * 
	 * @param evidenceId
	 * @param resourceLockState
	 * @param resourceIds
	 * @author: Fyh - 1009173473@qq.com 
	 * @date: Created on 2019年3月20日 上午9:24:32
	 */
	@Transactional(readOnly = false)
	public void resourceLogging(final String evidenceId, final ResourceLockState resourceLockState,
			List<String> resourceIds) {
		resourceLogMapper.stockoutResourceLogging(evidenceId, resourceLockState.getValue(), resourceIds);
	}

	@Transactional(readOnly = false)
	public void stockoutResourceLoggingBySkuIdAndEid(String evidenceId, Integer resourceLockState, Integer skuId) {
		resourceLogMapper.stockoutResourceLoggingBySkuIdAndEid(evidenceId, resourceLockState, skuId);
	}

	@Transactional(readOnly = false)
	public void stockoutResourceLoggingByErpSku(final String erpResId, final String evidenceId,
			Integer resourceLockState) {
		resourceLogMapper.stockoutResourceLoggingByErpSku(evidenceId, erpResId, resourceLockState);
	}

	@Transactional(readOnly = false)
	public void stockoutResourceLoggingByErpResSku(String evidenceId, String erpResId, Integer skuId,
			Integer resourceLockState) {
		resourceLogMapper.stockoutResourceLoggingByErpResSku(evidenceId, erpResId, skuId, resourceLockState);
	}

	public void updateLogEid(String fromEvidenceId, String toEvidenceId) {
		resourceLogMapper.updateLogEid(fromEvidenceId, toEvidenceId);
	}

}